package com.expert.conrtoller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.expert.annotations.RoleAnnotation;
import com.expert.dto.AddNoticeDTO;
import com.expert.dto.UpdateMenuDTO;
import com.expert.enums.RoleLevel;
import com.expert.pojo.Notice;
import com.expert.result.Result;
import com.expert.service.NoticeService;
import com.expert.vo.NoticeVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName: NoticeController
 * @Author: cxs
 * @Date: 2024/03/18 13:36
 * @Description:
 **/
@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeController {

    @Autowired
    NoticeService noticeService;
    /**
     * 添加通知功能
     * @param addNoticeDTO 添加通知
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @PostMapping("/addNotice")
    public Result addNotice(@RequestBody AddNoticeDTO addNoticeDTO){
      log.info("添加通知:{}",addNoticeDTO);

      this.noticeService.addNotice(addNoticeDTO);
      return Result.success();
    }

    /**
     * 分页查询通知消息
     * @param page
     * @param pageSize
     * @param beginTime
     * @param noticeTitle
     * @param noticeType
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/page")
    public Result<Page<NoticeVo>> pageNotice(Integer page, Integer pageSize,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                                             String noticeTitle, Integer noticeType){
        log.info("分页查询通知");

       Page<NoticeVo> noticeVoPage=this.noticeService.pageNotice(page,pageSize,noticeTitle,noticeType,beginTime,endTime);
        return Result.success(noticeVoPage);
    }

    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    @RoleAnnotation(level = RoleLevel.USER)
    @GetMapping("/{id}")
    public Result<NoticeVo> getInfoById(@PathVariable("id")Long id){
        log.info("根据id查询通知：{}",id);
        NoticeVo noticeVo=this.noticeService.getInfoById(id);
        return Result.success(noticeVo);
    }

    /**
     * 删除通知消息
     * @param ids
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @DeleteMapping("/delete")
    public Result deleteNotice(@RequestParam List<Long> ids){
        log.info("删除通知：{}",ids);
        this.noticeService.deleteNotice(ids);
        return Result.success();
    }


    /**
     * 分页查询用户的通知
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/pageUserNotice")
    public Result<Page<NoticeVo>> pageNotice(Integer page, Integer pageSize){
        log.info("分页查询通知");
        Page<NoticeVo> noticeVoPage=this.noticeService.pageUserNotice(page,pageSize);
        return Result.success(noticeVoPage);
    }
    /**
     * 修改通知
     * @param notice
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @PutMapping("/update")
    public  Result updateMenu(@RequestBody Notice notice ){
        log.info("修改通知：{}",notice);
        this.noticeService.updateNotice(notice);
        return Result.success();
    }
}
